﻿using System.Collections.Generic;
using JinJiang.BookingEngine.Services;

namespace HCRS.Implementation
{
    using Common;
    using HCRS.DBACL;
    using System;
    using System.Text;

    public sealed class Functions
    {
        private CommonWeb _CommonWeb = new CommonWeb();
        private DataAccessLayer _DataAccessLayer = new DataAccessLayer();
        private Email _Email = new Email();
        private HotelClass _HotelClass = new HotelClass();

        public static bool IsInt(string strVal)
        {
            if ((strVal == null) || (strVal.Length == 0))
            {
                return false;
            }
            if (((strVal == ".") || (strVal == "-")) || (strVal == "-."))
            {
                return false;
            }
            if (strVal.Substring(0, 1) == "-")
            {
                strVal = strVal.Remove(0, 1);
            }
            foreach (char ch in strVal)
            {
                if ((ch < '0') || (ch > '9'))
                {
                    return false;
                }
            }
            return true;
        }

        public void SendHTMLFax(string msgbody, string title, string[] mailTo, string mailFrom, string alias)
        {
            for (int i = 0; i < mailTo.Length; i++)
            {
                string[] strArray = this._DataAccessLayer.RunSqlStrings("select code from cc_emailsetting where [domain]='" + mailTo[i].Split(new char[] { '@' })[1] + "'");
                string coding = "GB2312";
                string errorMsg = this._DataAccessLayer.ErrorMsg;
                if ((strArray != null) && (strArray.Length > 0))
                {
                    coding = strArray[0];
                }
                List<string> receivers = new List<string> { mailTo[i] };
                StringBuilder builder = new StringBuilder();
                builder.Append("<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd><html xmlns=http://www.w3.org/1999/xhtml>");
                builder.Append("<head>");
                if (coding == "GB2312")
                {
                    builder.Append("<meta http-equiv=Content-Type content=text/html; charset=GB2312/>");
                }
                else
                {
                    builder.Append("<meta http-equiv=Content-Type content=text/html; charset=utf-8 />");
                }
                builder.Append("<style type=text/css>");
                builder.Append("<!--");
                builder.Append("body,td,th {");
                builder.Append("font-size: 14px;");
                builder.Append("font-family: Arial, Helvetica, sans-serif;");
                builder.Append("}");
                builder.Append("-->");
                builder.Append("</style></head>");
                builder.Append("<body >");
                builder.Append("<table width=98% border=0 cellpadding=5 cellspacing=5 bgcolor=#FFFFFF align=center>");
                builder.Append("<tr>");
                builder.Append("<td>");
                if (coding == "GB2312")
                {
                    builder.Append(this._CommonWeb.utf82gb2312(msgbody));
                    title = this._CommonWeb.utf82gb2312(title);
                }
                else
                {
                    builder.Append(msgbody);
                }
                builder.Append("</td>");
                builder.Append("</tr>");
                builder.Append("</table>");
                builder.Append("</body>");
                builder.Append("</html>");
                if (NotificationService.SendMail(title, builder.ToString(), "", receivers))
                {
                    this._HotelClass.AddMail(title, mailTo[i], mailFrom, this._CommonWeb.VerifySQL(builder.ToString()), coding, 1, 1, 1, DateTime.Now, 0);
                }
                else
                {
                    this._HotelClass.AddMail(title, mailTo[i], mailFrom, this._CommonWeb.VerifySQL(builder.ToString()), coding, 1, 0, 1, DateTime.Now, 0);
                }
            }
        }

        /// <summary>
        /// 发送HTML格式EMail，根据domain encoding进行邮件内容和标题编码
        /// </summary>
        /// <param name="msgbody">邮件内容</param>
        /// <param name="title">邮件标题</param>
        /// <param name="mailTo">收件人邮箱字符串数组</param>
        /// <param name="mailFrom">显示发件人邮箱</param>
        /// <param name="alias">发件人显示</param>
        public void SendHTMLMail(string msgbody, string title, string[] mailTo, string mailFrom, string alias)
        {
            for (int i = 0; i < mailTo.Length; i++)
            {
                string[] strArray = this._DataAccessLayer.RunSqlStrings("select code from cc_emailsetting where [domain]='" + mailTo[i].Split(new char[] { '@' })[1] + "'");
                string coding = "GB2312";
                string errorMsg = this._DataAccessLayer.ErrorMsg;
                if ((strArray != null) && (strArray.Length > 0))
                {
                    coding = strArray[0];
                }
                List<string> receivers = new List<string> { mailTo[i] };
                StringBuilder builder = new StringBuilder();
                builder.Append("<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd><html xmlns=http://www.w3.org/1999/xhtml>");
                builder.Append("<head>");
                if (coding == "GB2312")
                {
                    builder.Append("<meta http-equiv=Content-Type content=text/html; charset=GB2312/>");
                }
                else
                {
                    builder.Append("<meta http-equiv=Content-Type content=text/html; charset=utf-8 />");
                }
                builder.Append("<style type=text/css>");
                builder.Append("<!--");
                builder.Append("body,td,th {");
                builder.Append("font-size: 12px;");
                builder.Append("font-family: Arial, Helvetica, sans-serif;");
                builder.Append("}");
                builder.Append("-->");
                builder.Append("</style></head>");
                builder.Append("<body  bgcolor=#40587c>");
                builder.Append("<table width=98% border=0 cellpadding=5 cellspacing=5 bgcolor=#FFFFFF align=center>");
                builder.Append("  <tr>");
                builder.Append("    <td>");
                if (coding == "GB2312")
                {
                    builder.Append(this._CommonWeb.utf82gb2312(msgbody));
                    title = this._CommonWeb.utf82gb2312(title);
                }
                else
                {
                    builder.Append(msgbody);
                }
                builder.Append("</td>");
                builder.Append("  </tr>");
                builder.Append("</table>");
                builder.Append("<p>&nbsp;</p>");
                builder.Append("<p>&nbsp;</p>");
                builder.Append("</body>");
                builder.Append("</html>");

                if (NotificationService.SendMail(title, builder.ToString(), "", receivers))
                {
                    this._HotelClass.AddMail(title, mailTo[i], mailFrom, this._CommonWeb.VerifySQL(builder.ToString()), coding, 1, 1, 1, DateTime.Now, 0);
                }
                else
                {
                    this._HotelClass.AddMail(title, mailTo[i], mailFrom, this._CommonWeb.VerifySQL(builder.ToString()), coding, 1, 0, 1, DateTime.Now, 0);
                }
            }
        }

        /// <summary>
        /// 发送UTF8HTML格式EMail by field 2008-12-05 
        /// </summary>
        /// <param name="html">邮件内容</param>
        /// <param name="title">邮件标题</param>
        /// <param name="mailTo">收件人邮箱字符串数组</param>
        /// <param name="mailFrom">显示发件人邮箱</param>
        /// <param name="alias">发件人显示</param>
        public void SendHTMLUTF8(string html, string title, string[] mailTo, string mailFrom, string alias)
        {
            string msg = "";
            for (int i = 0; i < mailTo.Length; i++)
            {
                List<string> receivers = new List<string> { mailTo[i] };
                string coding = "UTF8";
                if (NotificationService.SendMail(title, html, "", receivers))
                {
                    this._DataAccessLayer.ExecInsertSQL("CC_Mail", "title,mailto,mailfrom,content,encode,prio,send,Err,type,addtime,userid".Split(new char[] { ',' }), "string,string,string,ntext,string,int,int,int,int,date,int".Split(new char[] { ',' }), new object[] { title, mailTo[i], mailFrom, html, coding, 1, 1, 1, 0, DateTime.Now, 0 }, out msg);
                }
                else
                {
                    this._DataAccessLayer.ExecInsertSQL("CC_Mail", "title,mailto,mailfrom,content,encode,prio,send,Err,type,addtime,userid".Split(new char[] { ',' }), "string,string,string,ntext,string,int,int,int,int,date,int".Split(new char[] { ',' }), new object[] { title, mailTo[i], mailFrom, html, coding, 1, 0, 1, 0, DateTime.Now, 0 }, out msg);
                }
            }
        }

        /// <summary>
        /// 发送纯文本格式EMail，根据domain encoding进行邮件内容和标题编码
        /// </summary>
        /// <param name="msgbody"></param>
        /// <param name="title"></param>
        /// <param name="mailTo"></param>
        /// <param name="mailFrom"></param>
        /// <param name="alias"></param>
        public void SendMail(string msgbody, string title, string[] mailTo, string mailFrom, string alias)
        {
            for (int i = 0; i < mailTo.Length; i++)
            {
                string[] strArray = this._DataAccessLayer.RunSqlStrings("select code from cc_emailsetting where [domain]='" + mailTo[i].Split(new char[] { '@' })[1] + "'");
                string coding = "GB2312";
                if ((strArray != null) && (strArray.Length > 0))
                {
                    coding = strArray[0];
                }
                if (coding == "GB2312")
                {
                    msgbody = this._CommonWeb.utf82gb2312(msgbody);
                    title = this._CommonWeb.utf82gb2312(title);
                }
                string[] strArray2 = new string[] { mailTo[i] };
                if (this._Email.SendMail(msgbody, title, strArray2, mailFrom, alias, "normal", false, coding))
                {
                    this._HotelClass.AddMail(title, mailTo[i], mailFrom, this._CommonWeb.VerifySQL(msgbody), coding, 1, 1, 1, DateTime.Now, 0);
                }
                else
                {
                    this._HotelClass.AddMail(title, mailTo[i], mailFrom, this._CommonWeb.VerifySQL(msgbody), coding, 1, 0, 1, DateTime.Now, 0);
                }
            }
        }

        public bool SendSMS(string smsContent, string mp)
        {
            if (((mp.Length == 11) && (smsContent.Length > 0)) && (smsContent.Length < 160))
            {
                //this._DataAccessLayer.ClearParameter();
                //this._DataAccessLayer.addNewParameter("@orig_mobile", mp, 50);
                //this._DataAccessLayer.addNewParameter("@content", smsContent, 500);
                //this._DataAccessLayer.RunStore("sms_send_Add");
                //this._DataAccessLayer.ClearParameter();
                //this._DataAccessLayer.addNewParameter("@orig_mobile", "13916666950", 50);
                //this._DataAccessLayer.addNewParameter("@content", smsContent, 500);
                //this._DataAccessLayer.RunStore("sms_send_Add");
                //if (this._DataAccessLayer.ErrorMsg.Length > 2)
                //{
                //    return false;
                //}
                if (!NotificationService.SendSms(smsContent + " 【锦江预订中心】", "", new List<string> { mp }))
                {
                    return false;
                }
                return true;
            }
            return false;
        }

        public static string strCal(string str, int num)
        {
            byte[] bytes = Encoding.Default.GetBytes(str);
            if (bytes.Length > num)
            {
                return (Encoding.Default.GetString(bytes, 0, num - 2) + "...");
            }
            return str;
        }
    }
}

